<?php

namespace User\Controller;

class IndexController extends BaseController {

  var $userObj;

  public function __construct() {
    parent::__construct();
    $this->userObj = D('User');
    $sysname = M("Sys")->where(array("name"=>"sys_name"))->getField("value");
    $this->assign('sys_name', $sysname);
    $this->assign('lang', $this->lang("zh"));



  }

  public function index() {
    foreach ($this->userMenu as $item) {
      header("location:{$item['url']}");
      die;
    }
  }
  /**
   * 用户登录\显示登录界面
   */
  public function login() {

          
            $isLogin = I("doSubmit");
            if (!empty($isLogin)) {
              $username = I("username");
              $password = I("password");
              $code = I("code");
              if (!empty($username) && !empty($password)) {
                $data['password'] = md5($password);
                $data[] = "(username = '{$username}' or phone = '{$username}')";
                $userInfo = $this->userObj->userInfo($data);
                if ($userInfo['id'] > 0) {
                  if($code =='' || $code != $_SESSION['code']){
                    $this->error('验证码错误！');
                    die;
                  }else{
                    $status = M("User")->where(array("id"=>$userInfo['id']))->getField("status");
                    if($status == 1){
                      $this->error('您的账号已被冻结！');
                    }
                    session('userId', $userInfo['id']);

                    //是否实名，初次登录赠送矿机
                    $shiming = M("User")->where(array("id"=>$userInfo['id']))->getField("shiming");
                    $userkj = M("Userkj")->where(array("user_id"=>$userInfo['id'],"kj_id"=>1))->getField("id");
                    if($shiming == 1 && !$userkj){
                      //送矿机
                      ypp_addkj($userInfo['id'],1,0);
                    }


                    $this->redirect('User/index');
                  }
                } else {
                  $this->error('用户名密码不匹配！');
                  die;
                }
      } else {
        $this->error('请输入用户名/密码!');
        die;
      }
    } else {
      $this->display('login');
    }
  }



  public function findPwd()
  {
    $isLogin = I("doSubmit");
    if (!empty($isLogin)) {
      $username = I("username");
      $password = I("password");
      $code = I("code");
      if(empty($username)){
        $this->error('请输入用户名！');
        die;
      }
      if(empty($password)|| strlen($password)<6){
        $this->error('请输入新密码，密码不能少于6位！');
        die;
      }
      if(empty($code)){
        $this->error('请输入email安全码！');
        die;
      }
      $data['username'] = $username;
      $userInfo = $this->userObj->userInfo($data);
      if($userInfo['id'] < 1){
        $this->error('该用户不存在！');
        die;
      }else{
        $id = $userInfo['id'];
        if($code != $_SESSION['em_code']){
          $this->error('您的email安全码错误！');
          die;
        }else{
          $re['password'] = md5($password);
          $this->userObj->updateUser($re, $id);
          $_SESSION['em_code'] = '';
          $this->error('密码已重置，请重新登陆！', U("Index/login"));
        }
      }
    }else{
      $this->display('findpwd');
    }
  }

  public function check(){
    if(IS_GET)
    {
      $user = M("User");
      $uname = I("get.uname");//$_GET["uname"]
      if($uname){
        $resuname = $user->where("username = '$uname'")->getField("id");
        if($resuname != ''){
          $this->ajaxReturn('','JSON'); /*json方式返回结果*/
        }else{
          $this->ajaxReturn('该用户不存在','JSON'); /*json方式返回结果*/
        }
      }
    }
  }


  public function sendEmail(){
    $sysname = M("Sys")->where(array("name"=>"sys_name"))->getField("value");
    $to = I("get.email");//$_GET['email']
    
    $code = rand(111111,999999);
    //$code = 123456;
    $_SESSION['em_code'] = $code;
    $mailset = M("Mailset")->where(array("id"=>1))->find();
    $title = $sysname." - Email安全码";
    $content = "您正在进行 ".$sysname." 的关键操作，您的安全码为：".$code;
    require  APP_PATH."/Common/Mail/PHPMailer.php";
    require  APP_PATH."/Common/Mail/SMTP.php";
    $mail = new \PHPMailer;
    $mail->IsSMTP();
    $mail->SMTPSecure = 'ssl';
    $mail->CharSet = 'UTF-8';
    $mail->SMTPAuth = true; //开启认证
    $mail->Port = 465; //网易为25
    $mail->Host = $mailset['host'];
    $mail->AddAddress($to);
    $mail->Username = $mailset['username']; //qq此处为邮箱前缀名 163为邮箱名
    $mail->Password = $mailset['password'];
    $mail->From = $mailset['from'];
    $mail->FromName = $mailset['fromname'];

    $mail->Subject = $title;
    $mail->Body = $content;
    $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; //当邮件不支持html时备用显示
    $mail->WordWrap = 80; // 设置每行字符串的长度
//$mail->AddAttachment("f:/test.png"); //可以添加附件
    $mail->IsHTML(true);
    $res = $mail->send();
    //$res = true;
    if($res){
      $msg['error'] = 0;
      $msg['msg'] = '发送成功!请登陆邮箱查看';
      $this->ajaxReturn($msg,'JSON'); /*json方式返回结果*/
    }else{
      $msg['error'] = 1;
      $msg['msg'] = '发送失败!请检查邮箱名是否正确';
      $this->ajaxReturn($msg,'JSON'); /*json方式返回结果*/
    }


  }
  /**
   * 退出登录
   */
  public function logout() {
    session('userId', NULL);
    $this->error('已退出登录！', U("Index/Index/index"));
  }



}
